package com.hongtao3.hsync;

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import com.hongtao3.hsync.ui.MainFrame;

public class Main {
	private  MainFrame mainFrame;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					Main m=new Main();
					m.systemTray();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}
	public static Image getTrayImage(){
	   return getResourcesImage("tray.png");
	}
	public static Image getResourcesImage(String imageName){
		return	Toolkit.getDefaultToolkit().getImage(Main.class.getResource("resources/"+imageName));
	}
	
	private void systemTray(){
	     if(java.awt.SystemTray.isSupported()){
	                java.awt.SystemTray st = java.awt.SystemTray.getSystemTray();
	               final PopupMenu jp=new PopupMenu();
	                MenuItem exitIt=new MenuItem("exit");
	                MenuItem aboutIt=new MenuItem("about");
	                jp.add(aboutIt);
	                jp.add(exitIt);
	                exitIt.addActionListener(new ActionListener() {
	                  	@Override
	                    public void actionPerformed(ActionEvent e) {
	                       System.exit(0);
	                    }
	                });
	                aboutIt.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							try {
								URI url = new URI("http://hsync.googlecode.com/");
								java.awt.Desktop.getDesktop().browse(url);
							} catch (Exception e1) {
								e1.printStackTrace();
							}
						}
					});
	                
	                final  java.awt.TrayIcon ti = new java.awt.TrayIcon(Main.getTrayImage(),"hsync",jp);
	                ti.setImageAutoSize(true);
	                try {
						st.add(ti);
					} catch (AWTException e1) {
						e1.printStackTrace();
					}
					
					ti.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							if(mainFrame==null){
								mainFrame=new MainFrame();
							}
							mainFrame.setVisible(true);
						}
					});
	                
		         
		     }
		   
    }
}
